var _=require('lodash');
var initialState = {
    lists: {},
    carts:[],
  };
  
  function newsList(state = initialState, action) {
    switch (action.type) {
       case "GETLISTS2":
             return Object.assign({},state,{lists: action.data});
        //这里action.data而不是state.data是因为：这个data是type为"GETLISTS"下面传过来的data，而increase和decrease只有type，没有data的传递。所以是state.data
        case "ADDTOCART"://添加产品与增加数量（+1）
            var posAdd =_.findIndex(state.carts,{'_id':action.data._id});
            if(posAdd === -1){
                action.data.quantity=1;
            }else{
                state.carts[posAdd].quantity = state.carts[posAdd].quantity +1;
            };
            return {
                ...state,
                carts:[
                    ...state.carts,
                    action.data
                ]
            }
      default:
        return state;
    }
  }
  
  export default newsList;
  